<?php

namespace app\api\controller;

use app\admin\model\partner\stone\Log;
use app\common\controller\Api;
use app\common\model\ApiToken;
use Carbon\Carbon;
use fast\Random;
use think\Env;

/**
 * Token接口
 */
class Chaotian extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function getToken()
    {
        $appId = input('post.appid');
        $appSecret = input('post.appsecret');

        if ($appId != Env::get('api.appid') or $appSecret != Env::get('api.appsecret')) {
            $this->error('应用ID或应用密码不正确');

        }

        $token = Random::alpha(16);

        $tokenModel = new ApiToken();
        $data = [
            'token' => $token,
            'createtime' => Carbon::now()->getTimestamp(),
            'expiretime' => Carbon::now()->addHours(2)->getTimestamp(),
        ];
        $tokenModel->allowField(true)->save($data);

        $this->success('ok', $data);
    }

    public function getStoneCategory()
    {
        $token = $this->request->header('token');
        if (!ApiToken::check($token)) {
            $this->error('请求token不合法');
        }

        $data = [
            [
                'stone_type_id' => 1,
                'stone_type_name' => '绿石头'
            ],
            [
                'stone_type_id' => 2,
                'stone_type_name' => '蓝石头'
            ],
            [
                'stone_type_id' => 3,
                'stone_type_name' => '红石头'
            ],
        ];

        $this->success('ok', $data);

    }

    public function getUserStoneInfo()
    {
        $token = $this->request->header('token');
        if (!ApiToken::check($token)) {
            $this->error('请求token不合法');
        }

        $uuid = input('get.uuid');
        $user = \app\admin\model\User::get(['chaotian_uuid' => $uuid]);
        if (is_null($user)) {
            $this->error('不存在此用用户');
        }

        $this->success('ok', [
            [
                'stone_type_id' => 1,
                'stone_type_name' => 'green_stone_amount',
                'stone_amount' => $user->green_stone_amount,
            ],
            [
                'stone_type_id' => 2,
                'stone_type_name' => 'blue_stone_amount',
                'stone_amount' => $user->blue_stone_amount,
            ],
            [
                'stone_type_id' => 3,
                'stone_type_name' => 'red_stone_amount',
                'stone_amount' => $user->red_stone_amount,
            ],
        ]);
    }

    public function stoneConsume()
    {
        $token = $this->request->header('token');
        if (!ApiToken::check($token)) {
            $this->error('请求token不合法');
        }

        $uuid = input('post.uuid');
        $stoneTypeId = input('post.stone_type_id');
        $stoneAmount = input('post.stone_amount');
        $consumeInfo = input('post.consume_info');
        $businessId = input('post.business_id');
        $businessType = input('post.business_type');

        $stoneMap = [
            1 => 'green_stone_amount',
            2 => 'blue_stone_amount',
            3 => 'red_stone_amount'
        ];

        if (!in_array($stoneTypeId, array_keys($stoneMap)) or !(is_numeric($stoneAmount) and $stoneAmount > 0)) {
            $this->error('输入参数错误');
        }

        $user = \app\admin\model\User::get(['chaotian_uuid' => $uuid]);
        if (is_null($user)) {
            $this->error('不存在此用用户');
        }

        if ($user->{$stoneMap[$stoneTypeId]} >= $stoneAmount) {

        } else {
            $this->error('此类石头数量不够');
        }

        $user->{$stoneMap[$stoneTypeId]} -= $stoneAmount;
        $user->save();

        //@todo 记录到日志里
        $log = new Log();
        $log->allowField(true)->save([
            'type' => 2,
            'from_id' => $user->id,
            'stone_type' => $stoneTypeId,
            'amount' => $stoneAmount,
            'consumeinfo' => $consumeInfo,
            'business_id' => $businessId,
            'business_type' => $businessType,
        ]);

        $this->success('ok', '消费成功');

    }



}
